app.factory("snsService", ["basicHttp", function(basicHttp){
    var promise;
    var parseParams = function(options){
        var opts = [];
        for(var opt in options){
            var str = opt + "=" + options[opt];
            opts.push(str);
        }
        return opts.join("&");
    };
    var service = {
        //api/v1/user/GetProfile
        getProfile: function(){
            basicHttp.setAuthToken();
            promise = basicHttp.http.get(basicHttp.prefixUrl + "User/GetProfile").then(function(response){
                return response.data.data;
            });
            return promise;
        },
        //api/v1/Province/Get
        getProvince: function(){
            /*alert(basicHttp.prefixUrl + "Province/Get");*/
            promise = basicHttp.http.get(basicHttp.prefixUrl + "Province/Get").then(function(response){

                return response.data.data;
            });
            return promise;
        },
        getDrivingSchool: function(options){
            //api/v1/DrivingSchool/Get?cityId={cityId}
            var params = options?"?"+parseParams(options):"";
            promise = basicHttp.http.get(basicHttp.prefixUrl + "DrivingSchool/Get" + params).then(function(response){
                return response.data;
            });
            return promise;
        },

        getCarTypes: function(options){
            // api/v1/LearnDriving/GetCarTypes?drivingSchoolId={drivingSchoolId}
            var params = options?"?"+parseParams(options):"";
            promise = basicHttp.http.get(basicHttp.prefixUrl + "LearnDriving/GetCarTypes" + params).then(function(response){
                return response.data;
            });
            return promise;
        },

        getClassTypes: function(options){
            //api/v1/LearnDriving/GetClassTypes?learnDrivingCarTypeId={learnDrivingCarTypeId}
            var params = options?"?"+parseParams(options):"";
            promise = basicHttp.http.get(basicHttp.prefixUrl + "LearnDriving/GetClassTypes" + params).then(function(response){
                return response.data;
            });
            return promise;
        },

        getClassPrice: function(options){
            //api/v1/LearnDriving/GetPrice?learnDrivingClassTypeId={learnDrivingClassTypeId}
            var params = options?"?"+parseParams(options):"";
            promise = basicHttp.http.get(basicHttp.prefixUrl + "LearnDriving/GetPrice" + params).then(function(response){
                return response.data;
            });
            return promise;
        },

        apply: function(options){
            basicHttp.setAuthToken();
            //api/v1/LearnDriving/Apply
            promise = basicHttp.http({
                method: 'POST',
                url: basicHttp.prefixUrl + "LearnDriving/Apply",
                data: options
            }).then(function(response){
                return response.data;
            });
            return promise;
        },
        getClubs: function(options){
            basicHttp.setAuthToken();
            var params = options?"?"+parseParams(options):"";
            promise = basicHttp.http.get(basicHttp.prefixUrl  + "SNS/GetClubs" + params).then(function(response){
                return response.data;
            });
            return promise;
        },

        getEnrollmentProfiles: function(options){
            basicHttp.setAuthToken();
            //learndriving/GetEnrollmentProfiles
            promise = basicHttp.http.get(basicHttp.prefixUrl + "LearnDriving/GetEnrollmentProfiles").then(function(response){
                return response.data;
            });
            return promise;
        },
        getRecommendedClubs: function(){
            basicHttp.setAuthToken();
            promise = basicHttp.http.get(basicHttp.prefixUrl + "SNS/GetRecommendedClubs").then(function(response){
                return response.data;
            });
            return promise;
        },
        getHomePage: function(){
            basicHttp.setAuthToken();
            promise = basicHttp.http.get(basicHttp.prefixUrl + "SNS/GetHomePage").then(function(response){
                return response.data;
            });
            return promise;
        },
        getUserClubs: function(){
            basicHttp.setAuthToken();
            promise = basicHttp.http.get(basicHttp.prefixUrl + "SNS/GetUserClubs").then(function(response){
                return response.data;
            });
            return promise;
        },
        searchClubs: function(options){
            basicHttp.setAuthToken();
            var params = options?"?"+parseParams(options):"";
            promise = basicHttp.http.post(basicHttp.prefixUrl + "SNS/SearchClubs" + params).then(function(response){
                return response.data;
            });
            return promise;
        },
        getClub: function(snsClubId){
            basicHttp.setAuthToken();
            promise = basicHttp.http.get(basicHttp.prefixUrl + "SNS/GetClub?snsClubId=" + snsClubId).then(function(response){
                return response.data;
            });
            return promise;
        },

        exitClub: function(options){
            //api/v1/SNS/ExitClub?snsClubId={snsClubId}
            basicHttp.setAuthToken();
            var params = options?"?"+parseParams(options):"";
            promise = basicHttp.http({
                method: 'POST',
                url: basicHttp.prefixUrl + "SNS/ExitClub" + params
            }).then(function(response){
                return response.data;
            });
            return promise;
        },
        joinClub: function(options){
            //POST api/v1/SNS/JoinClub?snsClubId={snsClubId}
            basicHttp.setAuthToken();
            var params = options?"?"+parseParams(options):"";
            promise = basicHttp.http({
                method: 'POST',
                url: basicHttp.prefixUrl + "SNS/JoinClub" + params
            }).then(function(response){
                return response.data;
            });
            return promise;
        },
        //SNS/GetTopicsAfterModified?snsClubId={snsClubId}&modifiedDecimal={modifiedDecimal}&takeCount={takeCount}
        getTopicsAfterModified: function(options){
            basicHttp.setAuthToken();
            var params = options?"?"+parseParams(options):"";
            console.log("++++++++++++++++")
            console.log(options)
            promise = basicHttp.http.get(basicHttp.prefixUrl + "SNS/GetTopicsAfterModified" + params).then(function(response){
                console.log(response)
                return response.data;
            });
            return promise;
        },
        getTopics: function(options){
            basicHttp.setAuthToken();
            var params = options?"?"+parseParams(options):"";
            promise = basicHttp.http.get(basicHttp.prefixUrl + "SNS/GetTopics" + params).then(function(response){
                return response.data;
            });
            return promise;
        },
        getTopic: function(snsTopicId){
            basicHttp.setAuthToken();
            promise = basicHttp.http.get(basicHttp.prefixUrl + "SNS/GetTopic?snsTopicId=" + snsTopicId).then(function(response){
                return response.data;
            });
            return promise;
        },
        //POST api/v1/SNS/RemoveTopic?snsTopicId={snsTopicId}
        removeTopic: function(options){
            basicHttp.setAuthToken();
            var params = options?"?"+parseParams(options):"";
            promise = basicHttp.http({
                method: 'POST',
                url: basicHttp.prefixUrl + "SNS/RemoveTopic" + params
            }).then(function(response){
                return response.data;
            });
            return promise;
        },
        removeReply: function(options){
            basicHttp.setAuthToken();
            var params = options?"?"+parseParams(options):"";
            promise = basicHttp.http({
                method: 'POST',
                url: basicHttp.prefixUrl + "SNS/RemoveReply" + params
            }).then(function(response){
                return response.data;
            });
            return promise;
        },
        getReplies: function(options){
            basicHttp.setAuthToken();
            var params = options?"?"+parseParams(options):"";
            promise = basicHttp.http.get(basicHttp.prefixUrl + "SNS/GetReplies" + params).then(function(response){
                return response.data;
            });
            return promise;
        },
        getReply: function(snsReplyId){
            basicHttp.setAuthToken();
            promise = basicHttp.http.get(basicHttp.prefixUrl + "SNS/GetReply?snsReplyId=" + snsReplyId).then(function(response){
                return response.data;
            });
            return promise;
        },
        reply: function(params){
            basicHttp.setAuthToken();
            var obj = {
                "snsTopicId": params.snsTopicId,
                "content": params.content,
                "imgUrls": []
            }
            promise = basicHttp.http({
                method: 'POST',
                url: basicHttp.prefixUrl + "SNS/Reply",
                data: obj
            }).then(function(response){
                return response.data;
            });
            return promise;
        },
        addTopic: function(params){
            basicHttp.setAuthToken();
            promise = basicHttp.http({
                method: 'POST',
                url: basicHttp.prefixUrl + "SNS/AddTopic",
                data: params
            }).then(function(response){
                return response.data;
            });
            return promise;
        },
        voteTopic: function(params){
            basicHttp.setAuthToken();
            promise = basicHttp.http({
                method: 'POST',
                url: basicHttp.prefixUrl + "SNS/VoteTopic",
                data: params
            }).then(function(response){
                return response.data;
            });
            return promise;
        },

        updateFile: function(file){
            basicHttp.setAuthToken();
            var formData = new FormData();

            formData.append('file_'+1, file);

            promise = basicHttp.http({
                method: 'POST',
                url: basicHttp.prefixUrl + "SNS/UploadImage",
                data: formData,
                //headers: {'Content-Type': 'multipart/form-data;charset=utf-8'}
                headers: {'Content-Type': undefined},
                transformRequest: angular.identity
            }).then(function(response){

                return response.data;
            });
            return promise;
        }
    }
    return service;
}]);